package com.xbang.attendance.web.action.system.warehouseproduct;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.product.Product;
import com.xbang.attendance.entity.stock.Inventory;
import com.xbang.attendance.entity.system.Warehouse;
import com.xbang.attendance.entity.system.WarehouseProductAgreement;
import com.xbang.attendance.service.InventoryService;
import com.xbang.attendance.service.SystemService;
import com.xbang.attendance.service.WarehouseService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.struts.support.SearchActionSupport;
import com.xbang.core.utils.DateUtils;

public class WarehouseProductManager extends SearchActionSupport {

	private static final long serialVersionUID = 6843098369301424790L;

	private static final Log logger = LogFactory.getLog(WarehouseProductManager.class);

	private Integer id;
	@Autowired
	private WarehouseService warehouseService;
	private WarehouseProductAgreement warehouseProductAgreement;

	private List<Warehouse> chooseWarehouse = new ArrayList<Warehouse>();

	private List<Product> chooseProduct = new ArrayList<Product>();

	@Autowired
	private SystemService systemService;

	private Warehouse warehouse;
	
	private Integer warehouseId;
	@Autowired
	private InventoryService inventoryService;

	private BigDecimal number;
	private List<WarehouseProductAgreement> warehouseProductAgreements = new ArrayList<WarehouseProductAgreement>();

	@Action("/system/warehouseproduct/add")
	public String add() throws Exception {
		chooseProduct = systemService.getObjects(Product.class);
		//chooseWarehouse = systemService.getObjects(Warehouse.class);
		warehouse = (Warehouse) systemService.getObject(Warehouse.class, warehouseId);
		
		
		return "add";
	}

	@Action("/system/warehouseproduct/save")
	public String save() throws Exception {
		if (warehouseProductAgreement.getId() != null) {
			
		//	systemService.update(warehouseProductAgreement);
		} else {
		//	warehouseProductAgreement = wareho
			Warehouse warehouse = (Warehouse) systemService.getObject(Warehouse.class,warehouseId);
			Product product = (Product) systemService.getObject(Product.class, warehouseProductAgreement.getProduct().getId());
			if(warehouseService.getWarehouseProductAgreement(warehouse, product) != null)
				{
					setErrorMessage("该产品 \""+product.getProductCode()+"---"+product.getProductName()+"\" 约定已经存在!");
					return ERROR;
				}
			warehouseProductAgreement.setLastModifyDate(new Date());
			warehouseProductAgreement.setWarehouse(warehouse);
			//warehouseService.getWarehouseProductAgreement(warehouse, product);
			Inventory inventory =new Inventory();
			inventory = inventory.initial(warehouse, product);
			inventory.setAgreementAmount(warehouseProductAgreement.getNumber());
			inventory.setAmount(warehouseProductAgreement.getNumber());
			
			systemService.save(inventory);
			systemService.save(warehouseProductAgreement);
			return "r:/system/warehouseproduct/detail/?warehouseId="+warehouseId;
		}

		updateAgreementAmount(warehouseProductAgreement);

		return "r:/system/warehouseproduct/detail/?warehouseId="+warehouseProductAgreement.getWarehouse().getId();
	}

	@Action("/system/warehouseproduct/${id}/edit")
	public String edit() throws Exception {
		//chooseProduct = systemService.getObjects(Product.class);
		//chooseWarehouse = systemService.getObjects(Warehouse.class);
		warehouseProductAgreement = (WarehouseProductAgreement) systemService.getObject(WarehouseProductAgreement.class, id);
		return "add";
	}

	@Action("/system/warehouseproduct/${id}/delete")
	public String delete() throws Exception {
		if (id != null) {
			warehouseProductAgreement = (WarehouseProductAgreement) systemService.getObject(WarehouseProductAgreement.class, id);
			systemService.delete(warehouseProductAgreement);
		}
		return "r:/system/warehouseproduct/list";
	}

	/**
	 * 描述：更新库存的"约定数量"
	 */
	private void updateAgreementAmount(WarehouseProductAgreement warehouseProductAgreement) throws Exception {
		BigDecimal currentCount = warehouseProductAgreement.getNumber();
		warehouse = (Warehouse) systemService.getObject(Warehouse.class, warehouseProductAgreement.getWarehouse().getId());
		Product product = (Product) systemService.getObject(Product.class, warehouseProductAgreement.getProduct().getId());
		
		WarehouseProductAgreement wpa = (WarehouseProductAgreement) systemService.get(WarehouseProductAgreement.class, warehouseProductAgreement.getId());
		
		BigDecimal bigDecimal = wpa.getNumber();
		//System.out.println(wpa.getNumber());
		Inventory inventory = inventoryService.getInventoryByParameters(warehouse, product);
		//System.out.println(warehouseProductAgreement.getNumber());
		BigDecimal updateDe = new BigDecimal(0.00);
		//database number > submit number
		if(bigDecimal.doubleValue() > currentCount.doubleValue()){
			updateDe = bigDecimal.subtract(currentCount);
			inventory.setAmount(inventory.getAmount().subtract(updateDe));
			inventory.setAgreementAmount(currentCount);
			wpa.setNumber(currentCount);
		}
		//database number < sumbmit number
		if(currentCount.doubleValue() > bigDecimal.doubleValue()){
			updateDe = currentCount.subtract(bigDecimal);
			inventory.setAmount(inventory.getAmount().add(updateDe));
			inventory.setAgreementAmount(currentCount);
			wpa.setNumber(currentCount);
		} 
		// database number == submit number don't do nothing....
		wpa.setLastModifyDate(new Date());
		inventoryService.update(inventory);
		systemService.update(wpa);
	}

	@Override
	public void resetCustomerSearchCriteria() {
		// TODO Auto-generated method stub

	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public SystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}

	public List<WarehouseProductAgreement> getWarehouseProductAgreements() {
		return warehouseProductAgreements;
	}

	public void setWarehouseProductAgreements(List<WarehouseProductAgreement> warehouseProductAgreements) {
		this.warehouseProductAgreements = warehouseProductAgreements;
	}

	public WarehouseProductAgreement getWarehouseProductAgreement() {
		return warehouseProductAgreement;
	}

	public void setWarehouseProductAgreement(WarehouseProductAgreement warehouseProductAgreement) {
		this.warehouseProductAgreement = warehouseProductAgreement;
	}

	public List<Warehouse> getChooseWarehouse() {
		return chooseWarehouse;
	}

	public void setChooseWarehouse(List<Warehouse> chooseWarehouse) {
		this.chooseWarehouse = chooseWarehouse;
	}

	public List<Product> getChooseProduct() {
		return chooseProduct;
	}

	public void setChooseProduct(List<Product> chooseProduct) {
		this.chooseProduct = chooseProduct;
	}

	public InventoryService getInventoryService() {
		return inventoryService;
	}

	public void setInventoryService(InventoryService inventoryService) {
		this.inventoryService = inventoryService;
	}

	public Warehouse getWarehouse() {
		return warehouse;
	}

	public void setWarehouse(Warehouse warehouse) {
		this.warehouse = warehouse;
	}

	public Integer getWarehouseId() {
		return warehouseId;
	}

	public void setWarehouseId(Integer warehouseId) {
		this.warehouseId = warehouseId;
	}
	

	

}
